package com.example.activity;

import com.example.model.HistoryListAdapter;
import com.example.model.HistoryModel;
import com.example.model.LocationListAdapter;
import com.example.model.LocationModel;
import com.example.test.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class HistoryView extends Activity  {
	private ListView historyList;
	private HistoryModel hisModel;
	private Cursor mCursor;
	private ImageButton back;
	private int History_ID = 0;
	private View itemView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.history_view);
		
		hisModel=new HistoryModel(this);
		mCursor=hisModel.select();
		
		historyList=(ListView)findViewById(R.id.list_history);
		historyList.setAdapter(new HistoryListAdapter(this,mCursor));
		historyList.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long id) {
				itemBackChanged(view);
				mCursor.moveToPosition(position);
				
				
			}
			
		});
		historyList.setOnItemLongClickListener(new OnItemLongClickListener()
		{

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View view,
					int position, long id) {
				
				mCursor.moveToPosition(position);
				String url=mCursor.getString(1);
				String curLoc=mCursor.getString(4);
				Intent intent = new Intent(HistoryView.this, BrowserView.class);
				Bundle bundle = new Bundle();
				
				System.out.printf(url, curLoc);
				bundle.putString("CURLOC", curLoc);
				bundle.putString("URL", url);

				intent.putExtras(bundle);
				startActivity(intent);
				
				return false;
			}
			
		});
		back=(ImageButton)findViewById(R.id.btn_back_history);
		back.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v) {
				finish();
				
			}
			
		});
	}
	public void delete()
	{
		hisModel.delete(History_ID);
		mCursor.requery();
		historyList.invalidateViews();
		
		Toast.makeText(this, "Delete Successed!", Toast.LENGTH_SHORT).show();
	}
	private void itemBackChanged(View view) {

		view.setBackgroundResource(android.R.color.holo_red_light);

		itemView = view;}
}
